package auto

import (
	"fmt"
	logger "github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"os"
	"os/signal"
	"syscall"
)

var (
	configFilePath = ""
	UriRequestCmd  = &cobra.Command{
		Use:   "request",
		Short: "request",
		Long:  "test service uri is arrived",
		Run: func(c *cobra.Command, args []string) {
			signalChan := make(chan os.Signal, 1)
			signal.Notify(signalChan, os.Interrupt, syscall.SIGINT)
			signal.Notify(signalChan, os.Interrupt, syscall.SIGQUIT)
			go func() {
				<-signalChan
				fmt.Println("\n接收到信号，程序即将退出...")
				os.Exit(0)
			}()
			config, error := Load(configFilePath)
			if error != nil {
				logger.Errorf("load %s config open is error %s", configFilePath, error)
				return
			} else if len(config.Scope) < 1 {
				logger.Errorf("servers 必须提供 %s ", error)
				return
			}
			beginRequest(config)
		},
	}
)

// init 解析命令参数
func init() {
	UriRequestCmd.PersistentFlags().StringVarP(&configFilePath, "config", "c", "uris.yaml", "config file path")
}
